package br.com.srv.teste;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;

import br.com.srv.util.DateHelper;

public class Cliente extends Thread {
	
	private static double timeSleepMin = 1000;
	private static double timeSleepMax = 25000;
	private static String IP_SERVIDOR = "localhost";
	private static int PORTA_SERVIDOR = 8000;
	private static int NUMERO_CLIENTES = 1;
	private static int NUMERO_TRANSMISSOES = 100;

	private String serial;
	static int countVeiculos = 1;
	
	Cliente(String serial) {
		
		this.serial = serial;		
		
	}
	
	public static void main(String a[]) {
		
		if (a.length > 0) {
			if (a[0] != null) {
				IP_SERVIDOR = a[0];
			} 
			if (a[1] != null) {
				PORTA_SERVIDOR = new Integer(a[1]);
			} 
			if (a[2] != null) {
				NUMERO_CLIENTES = new Integer(a[2]);
			}
		}

		
		for (int i = 0; i < NUMERO_CLIENTES; i++) {
			
		
		Cliente cliente1 = new Cliente("1");
		Cliente cliente2 = new Cliente("2");
		Cliente cliente3 = new Cliente("3");
		Cliente cliente4 = new Cliente("4");
		Cliente cliente5 = new Cliente("5");
		Cliente cliente6 = new Cliente("6");
		Cliente cliente7 = new Cliente("7");
		Cliente cliente8 = new Cliente("8");
		Cliente cliente9 = new Cliente("9");
		Cliente cliente10 = new Cliente("10");
		Cliente cliente11 = new Cliente("11");
		
			try {
	
				int x = 1500;
				cliente1.start();
				Double d = x*Math.random();
				Thread.sleep(d.intValue());
//				cliente2.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente3.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente4.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente5.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente6.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente7.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente8.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente9.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente10.start();
//				d = x*Math.random();
//				Thread.sleep(d.intValue());
//				cliente11.start();
				
			} catch (InterruptedException e) {
				System.out.println("Erro blabla bla");
			}
			
		}

	}
	
	private static String prepararCoordenada(Double coordenada) {
		NumberFormat formatter = new DecimalFormat("000.0000");
		String a = formatter.format(coordenada);
		
		return a.replace(',', '.');
	}
	
	public void run() {
		
		int count = 0;
		System.out.println(new Date() + " Veiculo: " + countVeiculos++);
		

		
		try {
			Socket socket = new Socket(IP_SERVIDOR, PORTA_SERVIDOR);
			OutputStream dataOutputStream = socket.getOutputStream();
			InputStream dataInputStream = socket.getInputStream();
			
			while (true && count < NUMERO_TRANSMISSOES) {
				
				try {
					Double valocidade = Math.random()*200;
				
					//03833.0490W
					String latStr = "03833." + new Double(Math.random()*9999).intValue() + "W";
					//0346.5874S
					String lngStr = "0346." + new Double(Math.random()*9999).intValue() + "S";
					
//					int codMotorista = new Double((Math.random()*3)+1).intValue();
					int codMotorista = 3;
	
//					String mensagem = "AT+CREG?";
//					String mensagem = serial+"#" + latStr + "#" + lngStr + "#"
//					+ valocidade.intValue() + "#-127#"+DateHelper.parseString("ddMMyy hhmmss", new Date())+"|";//"#mot:" + codMotorista +"|";
					
					String mensagem = "9#03836.7174W#0346.1239S#0.75#-127#060109 205501#gui:1|";
					
					if ((count % 5) == 0) {
						mensagem = "+++";
					} else if ((count % 3) == 0){
						mensagem = "AT";
					}
											
					dataOutputStream.write(mensagem.getBytes());
					dataOutputStream.flush();
					
					int i = 1;
					while (dataInputStream.available() == 0 && i++ < 50) {
						Thread.sleep(100L);
					}
					
					if (i < 50) {
						byte bytes[] = new byte[dataInputStream.available()];
						if(dataInputStream.available() > 0) {
							dataInputStream.read(bytes);
						}					
						System.out.println(serial + " :: " + count++ + " :: " + new String(bytes));
					} else {
						System.out.println(serial + " :: " + count++ + " :: " + new String("ignorado"));
					}
					
					Double timeSleep = Math.random()*timeSleepMax;
					
					if (timeSleep.intValue() < timeSleepMin) {
						timeSleep = timeSleepMin;
						System.out.println("====> tempo mínino!");
					}
					
					Thread.sleep(timeSleep.intValue());
	
				} catch (Exception e) {
					e.printStackTrace();
	
			    }
			}
			
			dataOutputStream.close();
			socket.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(new Date() + "Veiculo: " + --countVeiculos);
		
	}

}
